/* eslint no-undef: "error" */
import axios from 'axios'
import store from '../../store/index'
import router from '../../router/index'
import { Message } from 'element-ui'
axios.defaults.timeout = 60000
axios.defaults.withCredentials = true
axios.interceptors.request.use((config) => {
  // const token = getCookie ('session') // 获取Cookie
  // config.data = JSON.stringify(config.data)
  // if (store.state.logoData.userId) {
  //   config.headers.userId = store.state.logoData.userId
  // }
  if (config.url !== '/api/sysUser/sysUserLogin' && store.state.userInfo.token && store.state.userInfo.token.length > 0) {
    config.headers.common['Authorization'] = store.state.userInfo.token
  }
  console.log(config)
  return config
}, (error) => {
  return Promise.reject(error)
})
axios.interceptors.response.use((res) => {
  if (res.data.code === '40400001' || res.data.code === '40400002' || res.data.code === '40400003' || res.data.code === '40400004' || res.data.code === '40400005' || res.data.code === '40400006' || res.data.code === '40400007' || res.data.code === '40400008') {
    Message({
      message: res.data.message,
      type: 'error'
    })
    router.push({ name: 'login' })
  }
  if (res.status === 404 || res.status === 403) {
    router.push({ name: 'noResources' })
  } else if (res.status === 500 || res.status === 502) {
    router.push({ name: 'noControl' })
  }
  return res
}, (error) => {
  if (error) {
    if (error.response.status === 404 || error.response.status === 403) {
      router.push({ name: 'noResources' })
    } else if (error.response.status === 500 || error.response.status === 502) {
      router.push({ name: 'noControl' })
    }
  }
  return Promise.reject(error)
})
export default axios
